package net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class KeepAliveDaemon extends Thread {
	private InetAddress address;
	private int port;
	private DatagramSocket socket;
	private boolean running = false;
	
	public KeepAliveDaemon(DatagramSocket socket, InetAddress address, int port){
		this.socket = socket;
		this.address = address;
		this.port = port;
	}
	
	public void run(){
		running = true;
		DatagramPacket keepAlive;
		while(running){
			try {
				Thread.sleep(10*1000);
				keepAlive = new KeepAlive().getMsgPacket(address, port);
				socket.send(keepAlive);
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (IOException e) {
				running = false;
			}
		}
	}
	
	public synchronized void stopIt(){
		running = false;
	}
	
	public boolean running(){
		return running;
	}
	

}
